// pages/set/index.js
const app = getApp();
const http = require('../../utils/http.js');
import { refreshPage } from '../../utils/common'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    bottomHeight: app.globalData.model.bottomHeight,
    userInfo: null,

    birthday: '1996.05.06',
    idnumber: '207',
    nameValue: '',
    isUpdate: false
  },
  onChooseAvatar(e) {
    const { avatarUrl } = e.detail
	// 设置视频源
		http.uploadFile('api/common/upload', {
			path: avatarUrl
		}).then(res => {
			if (res.code == 1) {
				this.setData({
				  "userInfo.avatar": res.data.fullurl
				})
				this.setProfile('avatar')
			}

		})
    

  },
  nameInput(e) {
    const nickName = e.detail.value
    this.setData({
      "userInfo.nickname": nickName
    })
    this.submitName()
  },
  outLogin() {
    wx.showModal({
      title: '',
      content: '确定退出登录？',
      complete: (res) => {
        if (res.confirm) {
          app.logout();
        }
      }
    })
  },
  //获取用户手机
  getPhoneNumber(e) {
    if (e.detail.encryptedData) {
      this.setData({
        havePhone: false
      })
      http.post('api/token/mobile',{code: e.detail.code}).then(res => {
          //业务代码
          // this.setProfile('phone')
          this.setData({
            ['userInfo.mobile']: res.data.mobile
          })
          this.setProfile('mobile')
      })
    }
  },
  submitName(){
	  if(this.data.userInfo.nickname == wx.getStorageSync('userInfo').nickname){return;}
    this.setProfile('nickname')
  },
  setProfile(name){
    var data = {
      [name]: this.data.userInfo[name]
    }
    http.post('api/user/profile',data).then(res => {
      if(res.code == 1){
        wx.showModal({
          title: '修改成功!',
          success: (r) => {
            wx.setStorageSync('userInfo',this.data.userInfo)
            refreshPage();
          }
        })
        
      }
      
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      userInfo: wx.getStorageSync('userInfo')
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})